package com.wl.controller;

import com.wl.util.RedisTemplateUtils;
import com.wl.entity.Person;
import com.wl.model.User;
import com.wl.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Controller
@RequestMapping("/test")
public class TestController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private JedisPool jedisPool;//注入JedisPool

    // http://localhost:8080/test/hello，跳转到500页面
    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String hello(){
        logger.info("in tests");
        String error = null;
        error.split(",");
        return "aaa";
    }

    // http://localhost:8080/test/getUser/1
    @RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)
    public ModelAndView getUser(@PathVariable(name="id") String id){
        logger.info("in getUser >>>>> " + id);
        ModelAndView model = new ModelAndView("user");
        User user = userService.getUserById(id);
        Jedis jedis = jedisPool.getResource();
        //存入键值对
        jedis.set("user",user.toString());
        //回收ShardedJedis实例
        jedis.close();
        model.addObject("user",user);
        return model;
    }

    // http://localhost:8080/test/getUserInfo?id=1
    @RequestMapping(value = "getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public String getUserInfo(@RequestParam String id){
        logger.error(">>>>>>>>>>" + id);
        User user = userService.getUserById(id);
        RedisTemplateUtils.setHash("user2","user", user.toString());
        return user.toString();
    }

    // http://localhost:8080/test/getPerson?id=1
    @RequestMapping(value = "getPerson", method = RequestMethod.GET)
    @ResponseBody
    public String getPerson(@RequestParam Integer id){
        logger.warn(">>>>>>>>>>" + id);
        Person p = userService.getPerson(id);
        return p.toString();
    }

    // http://localhost:8080/test/updatePersonById?id=2&name=你好
    @RequestMapping(value = "updatePersonById", method = RequestMethod.GET)
    @ResponseBody
    public int updatePersonById(@RequestParam Integer id,@RequestParam String name){
        logger.info(">>>>>>>>>>" + id);
        int count = userService.updatePersonById(id, name);
        logger.warn("====>" + count);
        return count;
    }

    // double/int/date参数类型绑定
}
